package dailytools.dailyui.core;

import dailytools.dailyui.controller.EditorController;
import dailytools.dailyui.controller.FrameworkController;
import dailytools.dailyui.controller.MainController;
import dailytools.dailyui.controller.ViewerController;
import dailytools.dailyui.internal.resource.FxmlUtil;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

public class StageManager {
    FrameworkController frameworkController;
    Map<UIController, Node> sceneMap = new HashMap<>();

    public void loadScenes() {
        Class<?> clazz = MainController.class;
        loadScene(clazz);
        loadScene(EditorController.class);
        loadScene(ViewerController.class);
    }

    private void loadScene(Class<?> clazz) {
        FXMLLoader mainLoader = FxmlUtil.getFxml(clazz);
        Node root = mainLoader.getRoot();
        UIController uiController = mainLoader.getController();
        uiController.setStageManager(this);
        sceneMap.put(uiController, root);
    }

    public void setScene(Class<?> clazz) {
        sceneMap.forEach((key, value) -> {
            if (clazz.isInstance(key)) {
                frameworkController.getContentContainer().getChildren().clear();
                frameworkController.getContentContainer().getChildren().add(value);
            }
        });
    }

    public void loadFramework(Stage stage) {
        FXMLLoader mainLoader = FxmlUtil.getFxml(FrameworkController.class);
        Node root = mainLoader.getRoot();
        frameworkController = mainLoader.getController();
        frameworkController.setStageManager(this);
        sceneMap.put(frameworkController, root);
        stage.setScene(new Scene(mainLoader.getRoot()));
        stage.show();

    }
}
